﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Collections;

using FCNS.KeepMoney.Util;
using FCNS.KeepMoney.Configuration;

using BrightIdeasSoftware;

namespace FCNS.KeepMoney.Forms
{
    public partial class PrintForm : ParentForm
    {
        public PrintForm()
        {
            InitializeComponent();
            Program.Translation.ApplyTo(this);
        }

        public void Init(ListView listView,string header,string footer,
            string watermark,int tabControlIndex)
        {
            this.Icon = Properties.Resources.rmb;

            listViewPrinter1.ListView = listView;
            tbHeader.Text = header;
            tbFooter.Text = footer;
            tbWatermark.Text = watermark;

            comboBoxStyle.Items.AddRange(Enum.GetNames(typeof(PrintStyle.TrintStyles)));
            string path = AppDefines.appPath + "printstyle";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            foreach (string str in Directory.GetFiles(path, ".kms"))
                comboBoxStyle.Items.Add(Path.GetFileNameWithoutExtension(str));

            comboBoxStyle.SelectedIndex = 0;

            OnPreview(null, EventArgs.Empty);
            SearchStyle();
            FillingStyle(string.Empty);

            tabControl1.SelectedIndex = tabControlIndex;
        }

        private void OnPreview(object sender, EventArgs e)
        {
            listViewPrinter1.Header = tbHeader.Text;
            listViewPrinter1.Footer = tbFooter.Text;
            listViewPrinter1.Watermark = tbWatermark.Text;

            listViewPrinter1.IsShrinkToFit = cbShrinkToFit.Checked;
            listViewPrinter1.IsListHeaderOnEachPage = cbListHeaderOnEveryPage.Checked;
            if (!cbUseGridLines.Checked)
                listViewPrinter1.ListGridPen = null;

            PrintStyle.ApplyStyle(listViewPrinter1, comboBoxStyle.SelectedIndex);

            printPreviewControl1.InvalidatePreview();
        }

        private void OnPageSetup(object sender, EventArgs e)
        {
            listViewPrinter1.PageSetup();
            OnPreview(null, EventArgs.Empty);
        }

        private void OnPrint(object sender, EventArgs e)
        {
           listViewPrinter1.PrintWithDialog();
        }

        private void OnPageChange(object sender, EventArgs e)
        {
            int pages = (int)numericUpDownPage.Value;

            switch (pages)
            {
                case 1:
                case 2:
                case 3:
                  printPreviewControl1.Rows = 1;
                   printPreviewControl1.Columns = pages;
                    break;
                default:
                    printPreviewControl1.Rows = 2;
                   printPreviewControl1.Columns = ((pages - 1) / 2) + 1;
                    break;
            }
        }

        private void radioButton50_CheckedChanged(object sender, EventArgs e)
        {
            printPreviewControl1.Zoom = 0.5;
        }

        private void radioButton100_CheckedChanged(object sender, EventArgs e)
        {
            printPreviewControl1.Zoom = 1;
        }

        private void radioButton200_CheckedChanged(object sender, EventArgs e)
        {
            printPreviewControl1.Zoom = 2;
        }

        private void radioButtonAuto_CheckedChanged(object sender, EventArgs e)
        {
            printPreviewControl1.Zoom = 1;
        }

        //样式设计
        private void SearchStyle()
        {
                //buttonStyleSave.Enabled = false;
                //buttonStyleDelete.Enabled = false;
            if (!Directory.Exists(AppDefines.stylePath))
            {
                Directory.CreateDirectory(AppDefines.stylePath);
                return;
            }

            comboBoxAllStyle.Items.Clear();
            foreach (string file in Directory.GetFiles(AppDefines.stylePath))
                comboBoxAllStyle.Items.Add(Path.GetFileNameWithoutExtension(file));

            if (comboBoxAllStyle.Items.Count > 0)
            {
                comboBoxAllStyle.SelectedIndex = 0;
                buttonStyleSave.Enabled = true;
                buttonStyleDelete.Enabled = true;
            }
        }

        private void FillingStyle(string styleName)
        {
            if (styleName == string.Empty)
                propertyGrid1.SelectedObject = listViewPrinter1;
            else
            {
                //Hashtable table=new Hashtable();
                //XmlDocument xml = new XmlDocument();
                //xml.Load(dir + comboBoxAllStyle.Text + ".xml");
                //foreach (XmlNode node in xml.FirstChild.ChildNodes)
                //    table[node.Name] = node.InnerText;

                //foreach (System.Reflection.PropertyInfo pi in listViewPrinter1.GetType().GetProperties())
                //{
                //    if (pi.Name == "ListView")
                //        continue;
                    
                //    pi.SetValue(listViewPrinter1,(object)table[pi.Name], null);
                //}
                //propertyGrid1.Refresh();
                //propertyGrid1.Invalidate();
                //propertyGrid1.Update();
            }
        }

        private void SaveStyle()
        {
            XmlDocument xml = new XmlDocument();
            XmlElement style = xml.CreateElement("Style");
            foreach (System.Reflection.PropertyInfo pi in listViewPrinter1.GetType().GetProperties())
            {
                if (pi.Name == "ListView")
                    continue;

                try
                {
                    XmlElement xe = xml.CreateElement(pi.Name);
                    xe.InnerText= pi.GetValue(listViewPrinter1, null).ToString();
                    style.AppendChild(xe);
                }
                catch {continue; }
            }
            xml.AppendChild(style);
            xml.Save(AppDefines.stylePath + "qq.xml");
        }

        private void OnNewStyle(object sender, EventArgs e)
        {
            TextForm s = new TextForm();
            s.Init("新增样式", "请输入样式名称", "");
            s.ShowDialog();
            if (string.IsNullOrEmpty(s.GetText))
                return;

            comboBoxAllStyle.Items.Add(s.GetText);
            comboBoxAllStyle.SelectedIndex = comboBoxAllStyle.Items.Count - 1;
        }

        private void OnSaveStyle(object sender, EventArgs e)
        {
            SaveStyle();
            MessageBox.Show("样式保存完成");
        }

        private void OnRemoveStyle(object sender, EventArgs e)
        {
            try
            {
                File.Delete(AppDefines.stylePath + comboBoxAllStyle.Text + ".xml");
            }
            catch { return; }
            SearchStyle();
            MessageBox.Show("样式移除完成");
        }

        private void OnStyleIndexChanged(object sender, EventArgs e)
        {
            FillingStyle(comboBoxAllStyle.Text);
        }
    }
}
